using System;
using System.Collections;
using System.Collections.Generic;
using Server;
using Server.Mobiles;
using Server.Network;
using Server.Regions;
using Server.Multis;
using Server.Gumps;
using Server.ContextMenus;
using Server.Targeting;
using Server.Engines.BulkOrders;


namespace Server.Items
{
	public class CommVendorTile : Item
	{
		[Constructable]
		public CommVendorTile() : this( 0x181D, 1154 )
		{
		}

		[Constructable]
		public CommVendorTile( int itemID, int hue ) : base( itemID )
		{
			Name = "a Vendor Tile";
			Hue = hue;
			Weight = 1.0;

			Movable = false;
		}

		public CommVendorTile( Serial serial ) : base( serial )
		{
		}

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );

			writer.Write( (int)0 ); //version
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );

			int version = reader.ReadInt();
		}
	}

	[FlipableAttribute( 0xE40, 0xE41 )]
	public class VendorBox : Container
	{
		public override int DefaultMaxItems{ get{ return 0; } }
		public override int DefaultMaxWeight{ get{ return 0; } }

		private Mobile m_Owner;

		public Mobile Owner
		{
			get{ return m_Owner; }
			set{ m_Owner = value; InvalidateProperties(); }
		}

		[Constructable]
		public VendorBox() : base( 0xE40 )
		{
			Weight = 1.0;
			Name = "a Showcase";

			m_Owner = null;
		}

		public VendorBox( Serial serial ) : base( serial )
		{
		}


		public override bool CheckLift( Mobile from, Item item, ref LRReason reject )
		{
			if ( from.AccessLevel < AccessLevel.GameMaster )		//  && !( m_Owner != null && from == m_Owner )
			{
				reject = LRReason.CannotLift;
				return false;
			}

			return base.CheckLift( from, item, ref reject );
		}

		public override bool IsAccessibleTo( Mobile m )
		{
			return true;
		}

		public override bool CheckItemUse( Mobile from, Item item )
		{
			if ( !base.CheckItemUse( from, item ) )
				return false;

			if ( item is Container || item is BulkOrderBook || item is CloneItem )
				return true;

			from.SendLocalizedMessage( 500447 ); // That is not accessible.
			return false;
		}

		public override void GetChildContextMenuEntries( Mobile from, List<ContextMenuEntry> list, Item item )
		{
			base.GetChildContextMenuEntries( from, list, item );

			IHardsellable hs = RootParent as IHardsellable;
			Mobile vendor = RootParent as Mobile;

			if ( hs == null || hs.ShopItems == null || !( hs.ShopItems.Count > 0 ) )
				return;

			TradeItem ti = VendorManager.GetTradeItem( hs, item );

			if ( ti == null )
				return;

			if ( ti != null )
			{
				if ( hs.IsOwner( from ) || from.AccessLevel > AccessLevel.Player )
				{
					list.Add( new EditItemEntry( vendor, item ) );
					list.Add( new RemoveItemEntry( from, ti ) );
				}

				if ( ti.Price >= 0 )
					list.Add( new BuyItemEntry( vendor, ti ) );

				if ( ti.Price != 0 && hs.AllowBid && ( from.AccessLevel > AccessLevel.Player || !hs.IsOwner( from ) ) )
					list.Add( new PlaceBidEntry( from, ti ) );
			}
		}

		private class PlaceBidEntry : ContextMenuEntry
		{
			private Mobile m_from;
			private TradeItem m_ti;

			public PlaceBidEntry( Mobile from, TradeItem ti ) : base( 0156 )
			{
				m_from = from;
				m_ti = ti;
			}

			public override bool NonLocalUse{ get{ return true; } }

			public override void OnClick()
			{
				Item item = (Item)m_ti.ItemObj;

				if ( item.Deleted )
					return;

				Owner.From.CloseGump( typeof( VendorBidGump ) );
				Owner.From.SendGump( new VendorBidGump( m_from, m_ti ) );
			}
		}

		private class RemoveItemEntry : ContextMenuEntry
		{
			private Mobile m_from;
			private TradeItem m_ti;

			public RemoveItemEntry( Mobile from, TradeItem ti ) : base( 0163 )
			{
				m_from = from;
				m_ti = ti;
			}

			public override bool NonLocalUse{ get{ return true; } }

			public override void OnClick()
			{
				Item item = (Item)m_ti.ItemObj;

				if ( item.Deleted )
					return;

				VendorManager.ToRemoveTradeItem( m_from, m_ti );
			}
		}

		private class BuyItemEntry : ContextMenuEntry
		{
			private Mobile m_vendor;
			private TradeItem m_ti;

			public BuyItemEntry( Mobile vendor, TradeItem ti ) : base( 6103 )
			{
				m_vendor = vendor;
				m_ti = ti;

				Enabled = ti.Price > 0;
			}

			public override bool NonLocalUse{ get{ return true; } }

			public override void OnClick()
			{
				Item item = (Item)m_ti.ItemObj;

				if ( m_ti.Price < 0 || item.Deleted )
					return;

				Owner.From.CloseGump( typeof( CommVendorBuyGump ) );
				Owner.From.SendGump( new CommVendorBuyGump( m_vendor, m_ti ) );
			}
		}

		private class EditItemEntry : ContextMenuEntry
		{
			private Mobile m_Vendor;
			private Item m_Item;

			public EditItemEntry( Mobile vendor, Item item ) : base( 5101 )
			{
				m_Vendor = vendor;
				m_Item = item;
			}

			public override bool NonLocalUse{ get{ return true; } }

			public override void OnClick()
			{
				if ( m_Item.Deleted )
					return;

				Owner.From.CloseGump( typeof( AddTradeItemGump ) );
				Owner.From.SendGump( new AddTradeItemGump( m_Vendor, m_Item ) );
			}
		}

		public override void GetChildNameProperties( ObjectPropertyList list, Item item )
		{
			base.GetChildNameProperties( list, item );

			IHardsellable hs = RootParent as IHardsellable;

			if ( hs == null || hs.ShopItems == null || !( hs.ShopItems.Count > 0 ) )
				return;

			TradeItem ti = VendorManager.GetTradeItem( hs, item );

			if ( ti == null )
				return;

			if ( item is IUsesRemaining )
				list.Add( 1060584, "{0}", ((IUsesRemaining)item).UsesRemaining );	// uses remaining: ~1_val~

			if ( !ti.IsForSale )
				list.Add( 1043307 ); // Price: Not for sale.
			else if ( ti.IsForFree )
				list.Add( 1043306 ); // Price: FREE!
			else
				list.Add( 1043304, ti.Price.ToString() ); // Price: ~1_COST~
		}


		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );

			writer.Write( (int) 0 ); // version
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );

			int version = reader.ReadInt();
		}
	}
}
